<script setup>
import { ref } from 'vue'

const knowledgeNodes = ref([
  {
    id: 1,
    title: '前端开发',
    type: 'tech',
    progress: 85,
    children: [
      { id: 11, title: 'HTML/CSS', progress: 90 },
      { id: 12, title: 'JavaScript', progress: 85 },
      { id: 13, title: 'Vue.js', progress: 80 }
    ],
    connections: [2, 4],
    description: '前端开发技术栈和最佳实践'
  },
  {
    id: 2,
    title: '设计模式',
    type: 'science',
    progress: 70,
    children: [
      { id: 21, title: '创建型模式', progress: 75 },
      { id: 22, title: '结构型模式', progress: 65 },
      { id: 23, title: '行为型模式', progress: 70 }
    ],
    connections: [1, 3],
    description: '软件设计中的常用设计模式'
  },
  {
    id: 3,
    title: '项目管理',
    type: 'life',
    progress: 75,
    children: [
      { id: 31, title: '敏捷开发', progress: 80 },
      { id: 32, title: '团队协作', progress: 75 },
      { id: 33, title: '时间管理', progress: 70 }
    ],
    connections: [2],
    description: '项目管理方法论和实践'
  },
  {
    id: 4,
    title: 'UI设计',
    type: 'art',
    progress: 65,
    children: [
      { id: 41, title: '色彩理论', progress: 70 },
      { id: 42, title: '布局设计', progress: 65 },
      { id: 43, title: '交互设计', progress: 60 }
    ],
    connections: [1],
    description: '用户界面设计原则和方法'
  }
])

const selectedNode = ref(null)
const showNodeDetails = ref(false)

const selectNode = (node) => {
  selectedNode.value = node
  showNodeDetails.value = true
}

const closeDetails = () => {
  showNodeDetails.value = false
}

const getNodeColor = (type) => {
  const colors = {
    tech: 'var(--knowledge-tech)',
    science: 'var(--knowledge-science)',
    art: 'var(--knowledge-art)',
    life: 'var(--knowledge-life)'
  }
  return colors[type]
}
</script>

<template>
  <div class="knowledge-map">
    <!-- 顶部操作栏 -->
    <div class="map-header">
      <h1 class="section-title">知识地图</h1>
      <div class="map-controls">
        <button class="knowledge-btn">
          <span>🔍</span>
          搜索知识点
        </button>
        <button class="knowledge-btn">
          <span>➕</span>
          添加知识点
        </button>
      </div>
    </div>

    <!-- 知识地图主视图 -->
    <div class="map-container knowledge-card">
      <!-- 知识节点网络 -->
      <div class="nodes-network">
        <div 
          v-for="node in knowledgeNodes"
          :key="node.id"
          class="knowledge-node"
          :style="{ '--node-color': getNodeColor(node.type) }"
          @click="selectNode(node)"
        >
          <div class="node-content">
            <div class="node-header">
              <h3>{{ node.title }}</h3>
              <div class="node-progress">
                <div 
                  class="progress-bar"
                  :style="{ width: node.progress + '%' }"
                ></div>
                <span class="progress-text">{{ node.progress }}%</span>
              </div>
            </div>
            
            <div class="node-children">
              <div 
                v-for="child in node.children"
                :key="child.id"
                class="child-node"
              >
                <span class="child-title">{{ child.title }}</span>
                <div class="child-progress">
                  <div 
                    class="progress-bar"
                    :style="{ width: child.progress + '%' }"
                  ></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 节点详情侧边栏 -->
      <div 
        v-if="showNodeDetails"
        class="node-details"
      >
        <div class="details-header">
          <h3>{{ selectedNode.title }}</h3>
          <button class="close-btn" @click="closeDetails">×</button>
        </div>
        
        <div class="details-content">
          <div class="detail-section">
            <h4>描述</h4>
            <p>{{ selectedNode.description }}</p>
          </div>
          
          <div class="detail-section">
            <h4>学习进度</h4>
            <div class="progress-bar-large">
              <div 
                class="progress-fill"
                :style="{ 
                  width: selectedNode.progress + '%',
                  background: getNodeColor(selectedNode.type)
                }"
              ></div>
              <span class="progress-text">{{ selectedNode.progress }}%</span>
            </div>
          </div>
          
          <div class="detail-section">
            <h4>子知识点</h4>
            <div class="children-list">
              <div 
                v-for="child in selectedNode.children"
                :key="child.id"
                class="child-item"
              >
                <div class="child-header">
                  <span>{{ child.title }}</span>
                  <span>{{ child.progress }}%</span>
                </div>
                <div class="child-progress-bar">
                  <div 
                    class="progress-fill"
                    :style="{ 
                      width: child.progress + '%',
                      background: getNodeColor(selectedNode.type)
                    }"
                  ></div>
                </div>
              </div>
            </div>
          </div>

          <div class="detail-actions">
            <button class="knowledge-btn">
              <span>📝</span>
              编辑知识点
            </button>
            <button class="knowledge-btn">
              <span>📚</span>
              学习资源
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部工具栏 -->
    <div class="map-footer">
      <div class="map-legend">
        <div class="legend-item">
          <span 
            class="legend-color"
            style="background: var(--knowledge-tech)"
          ></span>
          技术
        </div>
        <div class="legend-item">
          <span 
            class="legend-color"
            style="background: var(--knowledge-science)"
          ></span>
          理论
        </div>
        <div class="legend-item">
          <span 
            class="legend-color"
            style="background: var(--knowledge-art)"
          ></span>
          艺术
        </div>
        <div class="legend-item">
          <span 
            class="legend-color"
            style="background: var(--knowledge-life)"
          ></span>
          生活
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.knowledge-map {
  width: 100%;
}

.map-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.map-controls {
  display: flex;
  gap: 1rem;
}

.map-container {
  width: 100%;
  min-height: 600px;
  position: relative;
  display: flex;
}

.nodes-network {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(450px, 1fr));
  gap: 1.5rem;
  padding: 1.5rem;
  width: 100%;
}

.knowledge-node {
  border: 2px solid var(--node-color);
  border-radius: 12px;
  padding: 1.25rem;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;
}

.knowledge-node:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.node-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.node-progress {
  width: 100px;
  height: 6px;
  background: var(--background-dark);
  border-radius: 3px;
  overflow: hidden;
  position: relative;
}

.progress-bar {
  height: 100%;
  background: var(--node-color);
  transition: width 0.3s ease;
}

.progress-text {
  position: absolute;
  right: -30px;
  top: -5px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.node-children {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.child-node {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.child-title {
  flex: 1;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.child-progress {
  width: 60px;
  height: 4px;
  background: var(--background-dark);
  border-radius: 2px;
  overflow: hidden;
}

.node-details {
  width: 350px;
  min-width: 350px;
  border-left: 1px solid var(--background-dark);
  padding: 1.5rem;
  background: white;
}

.details-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text-secondary);
  cursor: pointer;
}

.detail-section {
  margin-bottom: 1.5rem;
}

.detail-section h4 {
  margin-bottom: 0.8rem;
  color: var(--text-primary);
}

.progress-bar-large {
  height: 8px;
  background: var(--background-dark);
  border-radius: 4px;
  overflow: hidden;
  position: relative;
  margin-bottom: 1rem;
}

.progress-fill {
  height: 100%;
  transition: width 0.3s ease;
}

.children-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.child-item {
  background: var(--background-dark);
  padding: 0.8rem;
  border-radius: 8px;
}

.child-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
}

.child-progress-bar {
  height: 4px;
  background: rgba(0, 0, 0, 0.1);
  border-radius: 2px;
  overflow: hidden;
}

.detail-actions {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.map-footer {
  margin-top: 2rem;
  padding: 1rem;
  background: white;
  border-radius: 12px;
  box-shadow: var(--card-shadow);
}

.map-legend {
  display: flex;
  justify-content: center;
  gap: 2rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.legend-color {
  width: 12px;
  height: 12px;
  border-radius: 3px;
}
</style> 